<template>
  <div class="common-table">
    <el-table
      ref="refsTable"
      v-loading="loading"
      :data="newData"
      border
      fit
      highlight-current-row
      style="width: 100%"
      v-bind="$attrs"
      v-on="$listeners"
    >
      <el-table-column
        v-for="(column, index) in columns"
        :key="column.prop || index"
        :prop="column.prop"
        :label="column.label"
        :width="column.width"
        :align="column.align || 'center'"
      >
        <template slot-scope="scope">
          <slot
            :name="column.prop"
            :row="scope.row"
            :$index="scope.$index"
          >{{ scope.row[column.prop] }}</slot>
        </template>
      </el-table-column>
      <el-table-column align="right">
        <template slot-scope="scope">
          <el-button size="small" type="success" @click="openDetail(scope.$index, scope.row)">查看</el-button>
        </template>
      </el-table-column>
      <slot name="append"></slot>
    </el-table>

    <div class="pagination-container" v-if="showPagination">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </div>
    <el-dialog :visible.sync="dialogFormVisible" custom-class="custom-table" width="50%">
      <el-table :data="detailData">
        <el-table-column prop="hungNo" label="勾号" width="180"></el-table-column>
        <el-table-column prop="gdName" label="股道名称" width="180"></el-table-column>
        <el-table-column prop="workMethodDict" label="作业类型"></el-table-column>
        <el-table-column prop="numOfTrain" label="车数"></el-table-column>
        <el-table-column prop="tip" label="备注"></el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import { getDataJobTicket, getDataPieChartData, postShiftPlanDepart } from '@/api/statistics'
export default {
  name: 'CommonTable',
  props: {
    columns: {
      type: Array,
      default: () => []
    },
    fetchData: {
      type: Function,
      required: true
    },
    queryData: {
      // 添加查询参数
      type: Object,
      default: () => ({})
    },
    immediate: {
      // 是否立即请求数据
      type: Boolean,
      default: true
    },
    showPagination: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      currentPage: 1,
      pagesize: 10,
      innerPageSize: this.pagesize,
      tableData: [],
      loading: false,
      total: 0,
      newData: [],
      dialogFormVisible: false,
      detailData: [],
      showdetail: false
    }
  },
  created() {
    if (this.immediate) {
      this.getTableData(this.pagesize, this.currentPage)
      this.getData(this.pagesize, this.currentPage)
    }
  },
  mounted() {
    this.$nextTick(() => {
      // 根据浏览器高度设置初始高度
      this.tableHeight = window.innerHeight - this.$refs.refsTable.$el.offsetTop - 150
      // 监听浏览器高度变化，修改表格高度
      window.onresize = () => {
        this.tableHeight = window.innerHeight - this.$refs.refsTable.$el.offsetTop - 150
      }
    })
  },
  methods: {
    openDetail(item, row) {
      console.log(row, 'rrr')
      if (row.workItem.hungItemList.length != 0) {
        console.log(15151)

        this.detailData = row.workItem.hungItemList
      }
      this.dialogFormVisible = true
      this.editRow = row
    },
    async getTableData(size, num) {
      if (!this.fetchData) return

      this.loading = true
      try {
        const params = {
          // page: this.currentPage,
          // size: this.pagesize,
          ...this.queryData
        }
        const res = await this.fetchData(params,size, num)
        // console.log(res.data, '----')
        if (res.code === 0) {
          // 添加状态判断
          this.tableData = res.data
          this.total = res.total || res.data.length
          this.$emit('success', res) // 成功回调
        } else {
          this.$message.error(res.message || '获取数据失败')
          this.$emit('error', res) // 失败回调
        }
      } catch (error) {
        console.error(error)
        // this.$message.error('系统错误')
        this.$emit('error', error)
      } finally {
        this.loading = false
        this.$emit('complete') // 完成回调
      }
    },
    getData(size, num) {
      // console.log(1112)

      let newobj = {
        page: num,
        size: size,
        obj: this.queryData
      }
      getDataJobTicket(newobj).then(res => {
        if (res.code == 0) {
          // console.log(res.data, 'ddd')
          this.total = res.data.total
          this.newData = res.data.records.map(v => {
            return {
              ...v,
              ...v.workItem,
              beginTime1: v.beginTime,
              endTime1: v.endTime,
              kxzxTime: v.kxzxTime,
              avgTime: v.avgTime
            }
          })
        }
      })
    },
    // 刷新方法
    refresh() {
      return this.getData(this.pagesize, this.currentPage)
    },
    // 重置方法
    reset() {
      this.getData(10, 1)
    },
    handleSizeChange(val) {
      this.innerPageSize = val
      this.getData(this.pagesize, this.currentPage)
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.getData(this.pagesize, this.currentPage)
    }
  },
  watch: {
    // queryData: {
    //   handler() {
    //     this.currentPage = 1
    //     this.getTableData()
    //   },
    //   deep: true
    // }
  }
}
</script>

<style lang="scss" scoped>
@import './style.scss';
</style>